package com.oktest.measure.item.work.provider

import android.view.View
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.oktest.measure.R
import com.oktest.measure.entity.Type
import com.oktest.measure.kt.setArrowSpin
import com.oktest.measure.ui.adapter.WorkTreeAdapter
import kotlinx.android.synthetic.main.item_work_type.view.*

/**
 * @Author hhh
 * @Date 2020/9/22
 */
class WorkTypeProvider : BaseNodeProvider() {
    override val itemViewType: Int = 2
    override val layoutId: Int = R.layout.item_work_type

    override fun convert(helper: BaseViewHolder, type: BaseNode) {
        if(type !is Type) return
        helper.itemView.apply {
            tvTypeTitle.text = type.name
            ivType.setArrowSpin(type, false)
            if (type.isDone()) {
                ivType.setImageResource(R.drawable.measure_ic_work_tree_type_green)
            } else {
                ivType.setImageResource(R.drawable.measure_ic_work_tree_type)
            }
        }
    }

    override fun convert(helper: BaseViewHolder, item: BaseNode, payloads: List<Any>) {
        for (payload in payloads) {
            if (payload is Int) {
                helper.itemView.apply {
                    ivType.setArrowSpin(item, true)
                }
            }
        }
    }

    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        getAdapter()?.expandOrCollapse(
            position,
            parentPayload = WorkTreeAdapter.EXPAND_COLLAPSE_PAYLOAD
        )
    }
}